<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">添加广告</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
        <el-input v-model="searchParam.name" placeholder="请输入广告名称" class="user-input"></el-input>
        <el-select v-model="searchParam.position" placeholder="请选择位置" class="user-input">
          <el-option v-for="item in positionOptions"
                     :key="item.key"
                     :label="item.value"
                     :value="item.key">
          </el-option>
        </el-select>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="广告名称" prop="name"></el-table-column>
        <el-table-column label="广告图片" prop="img"></el-table-column>
        <el-table-column label="广告图片">
          <template scope="scope">
            <img :src="scope.row.img" class="art-cover" @click="previewImg(scope.row.img)">
          </template>
        </el-table-column>
        <el-table-column label="链接地址" prop="link"></el-table-column>
        <el-table-column label="位置" prop="position_name"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="添加时间" prop="add_time"></el-table-column>
        <el-table-column label="发布时间" prop="repub_time"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="添加广告" :visible.sync="showAddModelFlag" size="small">
      <div class="input-group">
        <label><i>*</i>广告名称：</label>
        <div class="input-group-box"><el-input v-model="addParam.name" placeholder="请输入广告名称"></el-input></div>
      </div>
	    <div class="input-group">
        <label>广告图片：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="cover-img" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="getfile($event)">
          </div>
          <img :src="addParam.img.url" class="cover" v-if="addParam.img.url!==''">
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>链接地址：</label>
        <div class="input-group-box"><el-input v-model="addParam.link" placeholder="请输入链接地址"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>位置：</label>
        <el-select v-model="addParam.position" placeholder="请选择位置" class="user-input">
          <el-option v-for="item in positionOptions"
                     :key="item.key"
                     :label="item.value"
                     :value="item.key">
          </el-option>
        </el-select>
      </div>
	    <div class="input-group">
        <label><i>*</i>排序：</label>
        <div class="input-group-box">
          <el-input v-model="addParam.sort" placeholder="请输入排序"  type="number" value="0"></el-input>
        </div>
      </div>
	    <div class="input-group">
        <label><i>*</i>发布时间：</label>
        <el-date-picker
          v-model="addParam.repub_time"
          type="datetime"
          placeholder="请选择发布时间"
          format="yyyy-MM-dd HH:mm:ss"
          default-time="12:00:00">
        </el-date-picker>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改广告" :visible.sync="showUpdateModelFlag" size="small">
      <div class="input-group">
        <label><i>*</i>广告名称：</label>
        <div class="input-group-box"><el-input v-model="editParam.name" placeholder="请输入广告名称"></el-input></div>
      </div>
      <div class="input-group">
        <label>广告图片：</label>
        <div class="input-group-box cover-box">
          <div class="el-upload el-upload--picture-card">
            <i class="el-icon-plus"></i>
            <input type="file" id="cover-img2" accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" class="zi_upload_input" @change="editfile($event)">
          </div>
          <img :src="editParam.img.url" class="cover" v-if="editParam.img.url!==''">
        </div>
      </div>
	    <div class="input-group">
        <label><i>*</i>链接地址：</label>
        <div class="input-group-box"><el-input v-model="editParam.link" placeholder="请输入链接地址"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>位置：</label>
        <el-select v-model="editParam.position" placeholder="请选择位置" class="user-input">
          <el-option v-for="item in positionOptions"
                     :key="item.key"
                     :label="item.value"
                     :value="item.key">
          </el-option>
        </el-select>
      </div>
	    <div class="input-group">
        <label><i>*</i>排序：</label>
        <div class="input-group-box">
          <el-input v-model="editParam.sort" placeholder="请输入排序" type="number" value="0"></el-input>
        </div>
      </div>
	    <div class="input-group">
        <label><i>*</i>发布时间：</label>
        <el-date-picker
          v-model="editParam.repub_time"
          type="datetime"
          placeholder="请选择发布时间"
          format="yyyy-MM-dd HH:mm:ss"
          default-time="12:00:00">
        </el-date-picker>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      imgUrl: '',
      imgVisible: false,
      cover: {
        url: '',
        img: ''
      },
      positionOptions: [],
      searchParam: {
        id: '',
        name: '',
        position: ''
      },
      addParam: {
        name: '',
        img: {
          src: '',
          url: ''
        },
        link: '',
        position: '',
        sort: '',
        repub_time: ''
      },
      editParam: {
        id: '',
        name: '',
        img: {
          src: '',
          url: ''
        },
        link: '',
        position: '',
        sort: '',
        repub_time: ''
      }
    }
  },
  created () {
    this.getList()
    this.getPositionOptions()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        name: this.searchParam.name,
        position: this.searchParam.position
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.adsList), this.params, res => {
        this.listData = res
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.name = ''
      this.searchParam.img = ''
      this.searchParam.link = ''
      this.searchParam.position = ''
      this.searchParam.sort = ''
      this.searchParam.repub_time = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      let formData = new FormData()
      formData.append('name', this.addParam.name)
      formData.append('img', this.addParam.img.src)
      formData.append('link', this.addParam.link)
      formData.append('position', this.addParam.position)
      formData.append('sort', this.addParam.sort)
      formData.append('repub_time', helper.formatDateTime(this.addParam.repub_time))
      let configs = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      api.POST(helper.getManageApiUrl(apiConfig.addAds), formData, configs, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.name = ''
        this.addParam.link = ''
        this.addParam.position = ''
        this.addParam.sort = 0
        var cover = document.getElementById('cover-img')
        cover.value = ''
        this.addParam.img.url = ''
        this.addParam.repub_time = ''
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteAds), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (item) {
      this.editParam.id = item.id
      this.editParam.name = item.name
      this.editParam.img.src = item.img
      this.editParam.img.url = item.img
      this.editParam.link = item.link
      this.editParam.position = item.position
      this.editParam.sort = item.sort
      this.editParam.repub_time = item.repub_time
      this.showUpdateModelFlag = true
    },
    save () {
      let formData = new FormData()
      formData.append('id', this.editParam.id)
      formData.append('name', this.editParam.name)
      formData.append('img', this.editParam.img.src)
      formData.append('link', this.editParam.link)
      formData.append('position', this.editParam.position)
      formData.append('sort', this.editParam.sort)
      formData.append('repub_time', helper.formatDateTime(this.editParam.repub_time))
      let configs = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      api.POST(helper.getManageApiUrl(apiConfig.updateAds), formData, configs, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        var cover = document.getElementById('cover-img2')
        cover.value = ''
        this.editParam.img.url = ''
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: '修改失败',
          type: 'error',
          duration: 1500
        })
      })
    },
    getfile (event) {
      this.addParam.img.src = event.target.files[0]
      this.addParam.img.url = window.URL.createObjectURL(this.addParam.img.src)
    },

    editfile (event) {
      this.editParam.img.src = event.target.files[0]
      this.editParam.img.url = window.URL.createObjectURL(this.editParam.img.src)
    },
    previewImg (cover) {
      this.imgUrl = cover
      this.imgVisible = true
    },
    getPositionOptions () {
      var param = {
        position: 'ads'
      }
      api.GET(helper.getManageApiUrl(apiConfig.adsPositionOptions), param, res => {
        this.positionOptions = res
      }, res => {
        console.log(res)
      })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }

 .input-group-box .el-upload--picture-card {
   position: relative;
   width: 100px;
   height: 100px;
 }
 .input-group-box img.cover {
   width: 95px;
   height: 71px;
   display: block;
   margin-bottom: 10px;
 }
 .input-group-box .el-upload--picture-card i {
   position: absolute;
   top: 35px;
   left: 35px;
 }
 .el-upload--picture-card .zi_upload_input {
   position: absolute;
   display: inline-block;
   width: 100%;
   height: 100%;
   opacity: 0;
   top: 0;
   left: 0;
   cursor: pointer;
 }
 .cover-box .p-tip {
   display: inline-block;
   color: red;
   font-size: 12px;
   margin-top: 80px;
   margin-left: 10px;
 }
 .art-cover {
   width: 95px;
   height: 70px;
 }
</style>

